#include<iostream>
#include<string>
#include<fstream>
#include<sstream>

using namespace std;

class Date
{
public:
    Date(int x=0,int y=0,int z=0)
    :_x(x),_y(y),_z(z)
    {}
    friend ostream& operator<<(ostream& out,const Date& d1);
    friend istream& operator>>(istream& in,Date& d1);
    
    int _x;
    int _y;
    int _z;
    
};

ostream& operator<<(ostream& out,const Date& d1)
{
    out<<d1._x<<" "<<d1._y<<" "<<d1._z<<endl;
    return out;
}
istream& operator>>(istream& in,Date& d1)
{
    in>>d1._x>>d1._y>>d1._z;
    return in;
}

struct ServerInfo
{
char _address[64];
    //string _address;//向文件写入string对象
int _port;
Date _date;
};

//写文件读出来
//1.二进制读写
//2.文本读写

//1.二进制读写
class BinIO
{
public:
    BinIO(const char* filename="test.txt")
    :_filename(filename)
    {
        
    }
    
    void Write(const ServerInfo& winfo)
    {
        ofstream ofs;
        ofs.open(_filename,ofstream::out|ofstream::binary);
        ofs.write((char*)(&winfo), sizeof(winfo));
        ofs.close();
    }
    
    void Read(ServerInfo& rinfo)
    {
        ifstream ifs;
        ifs.open(_filename,ifstream::in|ifstream::binary);
        ifs.read((char*)(&rinfo), sizeof((rinfo)));
        ifs.close();
    }
private:
    string _filename;
};

//2.文本读写
class TextIO
{
public:
    TextIO(const char* filename="test.text")
    :_filename(filename)
    {
        
    }
    
    void Write(const ServerInfo& winfo)
    {
        ofstream ofs;
        ofs.open(_filename,ofstream::out);
        ofs<<winfo._address<<endl;
        ofs<<winfo._port<<endl;
        ofs<<winfo._date<<endl;
        ofs.close();
    }
    
    void Read(ServerInfo& rinfo)
    {
        ifstream ifs;
        ifs.open(_filename,ifstream::in);
        ifs>>rinfo._address;
        ifs>>rinfo._port;
        ifs>>rinfo._date;
        ifs.close();
    }
private:
    string _filename;
};
void test_1()
{
    ServerInfo sinfo={"https://chat18.aichatos.xyz/#/chat/1712799998218",8888,{2024,1,24}};
    BinIO b1;

    b1.Write(sinfo);
    
    ServerInfo s1;
    b1.Read(s1);
    cout<<s1._address<<"    "<<s1._port<<"  "<<s1._date<<endl;
}

void test_2()
{
    TextIO text;
//    ServerInfo sinfo={"https://chat18.aichatos.xyz/#/chat/1712799998218",8888,{2024,1,24}};
//    text.Write(sinfo);
    
    ServerInfo s2;
    text.Read(s2);
    cout<<s2._address<<"    "<<s2._port<<"  "<<s2._date<<endl;
}
void test_3()
{
    Date d(2024,1,24);
    ostringstream oss;
    oss<<d._x;
    oss<<d._y;
    oss<<d._z;
    cout<<oss.str()<<endl;
    
    istringstream iss(oss.str());
    Date d1;
    iss>>d1._x>>d1._y>>d1._z;
    cout<<d1._x<<d1._y<<d1._z<<endl;
    
    
}
int main()
{
    //test_2();
    test_3();
    return 0;
}
